#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2020/5/26 18:24
# @Author : wn 
# @File: macaddress.py
# @SoftWare: PyCharm
# @Site:
# @Table:
# ｡◕‿◕｡｡◕‿◕｡｡◕‿◕｡

'''
注意点：
因为address是 255.255.255.255，修改的时候没有携带原有的值，并报错，该用了id
'''



from django.shortcuts import render, HttpResponse, redirect
from Manager.models import *
from Rbac.models import *
from Manager.views.service import my_page
from django.http import JsonResponse
import json
from Django_Admin.enumType import *

from Rbac.utils.login_deco import *

@auth
def macaddress_list(request):
    # 当前在第几页
    off = request.GET.get('offset', None)
    # 一页几条数据
    lim = request.GET.get('limit', None)
    # 获取url后拼接的search
    search = request.GET.get('search', None)
    # 判断url后面是否拼接了search，没有，返回页面；有显示搜索条件的值
    if search is None:
        customerid = Customer.objects.all()
        return render(request, 'macaddress.html', {"customerobj": customerid})
    else:
        # 按搜索条件获取值
        if search:
            # 如果多个主键，这filter中，连写另一个值可以吗
            ma_list = Macaddress.objects.values().filter(macaddress__contains=search)
        else:
            ma_list = Macaddress.objects.values()
        # 把queryset转成列表[{},{}]
        ma_list = list(ma_list)
        for item in ma_list:
            # 联合主键，用id
            item['option'] = item['id']
            # 枚举值
            item['isactive'] = IsActive_choices.get(item['isactive'],None)
        result = my_page(ma_list, lim, off)
        return JsonResponse(result, safe=False)


# 添加，修改，删除
def macaddress_modify(request):
    ##拿到登录用户id
    userid = request.session["id"]
    isdelete = Customer.objects.filter(customerid=userid).filter(
        roles__permissions__url__endswith='macaddress_list(delete)')
    ismodify = Customer.objects.filter(customerid=userid).filter(
        roles__permissions__url__endswith='macaddress_list(modify)')
    isadd = Customer.objects.filter(customerid=userid).filter(
        roles__permissions__url__endswith='macaddress_list(add)')
    if request.method == 'GET':
        # 联合主键，id
        ma_id = request.GET['id']
        item = Macaddress.objects.values().filter(id=ma_id).first()
        # print('被修改的值：', item)
        if item:
            return JsonResponse(item, safe=False)
    else:
        if request.POST.get('delete', None):
            if isdelete:
                # 删除,id是函数携带的参数
                maid = request.POST.get('id')
                try:
                    Macaddress.objects.filter(id=maid).delete()
                    return HttpResponse('y')
                except Exception as e:
                    return HttpResponse(e)
            else:
                return HttpResponse("该用户无操作权限！")
        else:
            data = request.POST
            try:
                if json.loads(data['id'])[0]:
                    if Macaddress.objects.filter(id=json.loads(data['id'])[0]).count() >= 1:
                        # 修改
                        if ismodify:
                            Macaddress.objects.filter(id=json.loads(data['id'])[0]).update(
                                customerid=json.loads(data["customerid"])[0],
                                description=json.loads(data["description"])[0],
                                macaddress=json.loads(data["macaddress"])[0],
                                isactive=json.loads(data["isactive"])[0],
                            )
                            return HttpResponse("y")
                        else:
                            return HttpResponse("该用户无操作权限！")
                else:

                    if isadd:
                        # 新增
                        Macaddress.objects.create(
                            customerid=json.loads(data["customerid"])[0],
                            description=json.loads(data["description"])[0],
                            macaddress=json.loads(data["macaddress"])[0],
                            isactive=json.loads(data["isactive"])[0],
                        )
                        return HttpResponse("y")
                    else:
                        return HttpResponse("该用户无操作权限！")
            except Exception as e:
                return HttpResponse(e)
        return render(request, 'macaddress.html')
